<template>
    <div class="header_r flex" style="padding: 5px;">
        <MedicalPrintListView ref="MedicalPrintListView" />

        <div class="serachBox flex">
            <el-dropdown >
                <div class="flex">
                    <p style="width: 60px;">申领人</p> <el-icon>
                        <ArrowDown />
                    </el-icon>
                </div>
                <template #dropdown>
                    <el-dropdown-menu>
                        <el-dropdown-item command="申领人">申领人</el-dropdown-item>
                    </el-dropdown-menu>
                </template>
            </el-dropdown>
            <input type="text" v-model="searchValue">
            
        </div>
        <div class="serachBox flex">
            <el-dropdown >
                <div class="flex">
                    <p style="width: 60px;">申领物资</p> <el-icon>
                        <ArrowDown />
                    </el-icon>
                </div>
                <template #dropdown>
                    <el-dropdown-menu>
                        <el-dropdown-item command="申领物资">申领物资</el-dropdown-item>
                    </el-dropdown-menu>
                </template>
            </el-dropdown>
            <input type="text" v-model="searchValue2">
            
        </div>
        <button class="search" @click="getMylist">搜索</button>

        <div class="addbtn flex" v-loading.fullscreen.lock="fullscreenLoading" @click="exportExcel">
            <p>导出明细</p>
        </div>

        <!-- 先选年月 在下载领用单 -->
        <div class="addbtn flex" v-loading.fullscreen.lock="fullscreenLoading" @click="exportExcel" v-if="false">
            <p>下载领用单</p>
        </div>
        <div class="addbtn flex" @click="printHandle" v-if="false">
            <p>打印</p>
        </div>
        <!-- <div class="serachBox flex">
            <div class="flex">
                <p>申领物资</p>            
            </div>
            <input type="text" v-model="materialName">
        </div> -->
    </div>
    <div class="table_box">
        <el-table :data="list" border style="width: 100%;" scrollTo :row-class-name="tableRowClassName"
            :header-cell-style="headerCellStyle" :row-style="tableRowStyle" @selection-change="handleSelectionChange">

            <el-table-column type="selection" :selectable="selectable" width="55" />
            <el-table-column prop="requestor" label="申领人">
                <template #header>
                    <div class="header_icon">
                        <span>申领人</span>
                        <el-icon class="rotate">
                            <MoreFilled />
                        </el-icon>
                    </div>
                </template>
            </el-table-column>
            <el-table-column prop="materialName" label="申领物资">
                <template #header>
                    <div class="header_icon">
                        <span>申领物资</span>
                        <el-icon class="rotate">
                            <MoreFilled />
                        </el-icon>
                    </div>
                </template>
            </el-table-column>
            <el-table-column prop="batchNo" label="批次号">
                <template #header>
                    <div class="header_icon">
                        <span>批次号</span>
                        <el-icon class="rotate">
                            <MoreFilled />
                        </el-icon>
                    </div>
                </template>
            </el-table-column>
            <el-table-column prop="quantity" label="申领数量">
                <template #header>
                    <div class="header_icon">
                        <span>申领数量</span>
                        <el-icon class="rotate">
                            <MoreFilled />
                        </el-icon>
                    </div>
                </template>
            </el-table-column>
            <el-table-column prop="price" label="单价">
                <template #header>
                    <div class="header_icon">
                        <span>单价</span>
                        <el-icon class="rotate">
                            <MoreFilled />
                        </el-icon>
                    </div>
                </template>
            </el-table-column>
            <!-- <el-table-column prop="createdAt" label="出库时间">
                <template #header>
                    <div class="header_icon">
                        <span>出库时间</span>
                        <el-icon class="rotate">
                            <MoreFilled />
                        </el-icon>
                    </div>
                </template>
            </el-table-column> -->
            <el-table-column prop="remark" label="备注">
                <template #header>
                    <div class="header_icon">
                        <span>备注</span>
                        <el-icon class="rotate">
                            <MoreFilled />
                        </el-icon>
                    </div>
                </template>
            </el-table-column>
        </el-table>

        <!-- <el-table :data="list" border style="width: 100%;" scrollTo :row-class-name="tableRowClassName"
            :header-cell-style="headerCellStyle" :row-style="tableRowStyle" @selection-change="handleSelectionChange">
            <el-table-column type="index" label="序号" width="55" /> 
            <el-table-column prop="receiptCode" label="出库单">
                <template #header>
                    <div class="header_icon">
                        <span>出库单</span>
                        <el-icon class="rotate">
                            <MoreFilled />
                        </el-icon>
                    </div>
                </template>
            </el-table-column>
            <el-table-column prop="department" label="申领科室">
                <template #header>
                    <div class="header_icon">
                        <span>申领科室</span>
                        <el-icon class="rotate">
                            <MoreFilled />
                        </el-icon>
                    </div>
                </template>
            </el-table-column>
            <el-table-column prop="totalQuantity" label="数量">
                <template #header>
                    <div class="header_icon">
                        <span>数量</span>
                        <el-icon class="rotate">
                            <MoreFilled />
                        </el-icon>
                    </div>
                </template>
            </el-table-column>
            <el-table-column prop="totalValue" label="金额">
                <template #header>
                    <div class="header_icon">
                        <span>金额</span>
                        <el-icon class="rotate">
                            <MoreFilled />
                        </el-icon>
                    </div>
                </template>
            </el-table-column>
            <el-table-column prop="remark" label="备注">
                <template #header>
                    <div class="header_icon">
                        <span>备注</span>
                        <el-icon class="rotate">
                            <MoreFilled />
                        </el-icon>
                    </div>
                </template>
            </el-table-column>
            <el-table-column prop="createdBy" label="提交人">
                <template #header>
                    <div class="header_icon">
                        <span>提交人</span>
                        <el-icon class="rotate">
                            <MoreFilled />
                        </el-icon>
                    </div>
                </template>
            </el-table-column>
            <el-table-column prop="createdAt" label="出库时间">
                <template #header>
                    <div class="header_icon">
                        <span>出库时间</span>
                        <el-icon class="rotate">
                            <MoreFilled />
                        </el-icon>
                    </div>
                </template>
            </el-table-column>
            <el-table-column label="操作" width="110">
                <template #default="scope">
                    <el-button type="primary" style="padding: 0 10px;"
                        @click="viewDetails(scope.row )">查看明细</el-button>
                </template>
            </el-table-column>


        </el-table> -->
    </div>

    <el-config-provider :locale="locale">
        <div style="margin-top: 10px" class="pageBox flex">
            <el-pagination :total="total" :current-page="page" :page-size="pageSize" background
                layout="prev, pager, next, sizes" :page-sizes="[5, 10, 20, 30, 40, 50, 100]"
                @size-change="changePageSize" @current-change="changeCurrentPage" />
            <div class="flex">
                <p>第 {{ page }} 条</p>
                <p>共 {{ total }} 条数据</p>
            </div>
        </div>
    </el-config-provider>

</template>
<script>
import { httpService } from "@/utils/httpService";
import { ElConfigProvider, ElMessage, ElLoading } from "element-plus";
import { MoreFilled, ArrowDown } from "@element-plus/icons-vue";
// import zhCn from "element-plus/lib/locale/lang/zh-cn";
import { formatTime } from "@/utils/dateFormat"
import MedicalPrintListView from "../../components/medicalProduct/MedicalPrintListView.vue"


export default {
    name: "MedicalProductLogin",
    components: { ElMessage, ElLoading, MoreFilled, ArrowDown, ElConfigProvider, MedicalPrintListView },
    data() {
        return {
            page: 1,
            pageSize: 10,
            total: 0,
            list: [],
            requestor: "",
            materialName: "",
            searchTitle: "申领人",
            searchValue: "",
            searchValue2:""
        };
    },
    // setup() {
    //     return {
    //         locale: zhCn,
    //     };
    // },
    computed: {},
    mounted() {
        this.getMylist()

    },
    methods: {
        handleSelectionChange(val) {
            console.log(val);
            this.printList = val
        },

        printHandle() {
            if (this.printList.length > 0) {
                console.log(13213, this.printList);
                const itemsPerPage = 10; // 每页最多打印的条目数
                const totalPages = Math.ceil(this.printList.length / itemsPerPage); // 总页数
                const department = this.printList[0].department
                const requestor = this.printList[0].requestor
                const createdAt = this.printList[0].createdAt
                const createName = this.printList[0].createName

                const baseNumber = "Y" + String(this.printList[0].id).padStart(6, '0');    // 原始编号，如 "Y000001"

                // 提取编号中的数字部分
                const prefix = baseNumber.match(/^\D+/)[0]; // 提取非数字部分 "Y"
                const startNumber = parseInt(baseNumber.match(/\d+$/)[0], 10); // 提取数字部分 "000001"

                // 将数据分成每页的组
                const paginatedList = Array.from({ length: totalPages }, (_, i) =>
                    this.printList.slice(i * itemsPerPage, (i + 1) * itemsPerPage)
                );

                // 生成 HTML 内容
                const printContent = paginatedList.map((pageItems, pageIndex) => {
                    // 根据页码生成新的编号
                    const currentNumber = prefix + String(startNumber + pageIndex).padStart(6, '0'); // 保证编号长度为6位

                    return `
                    <div class="table-container">
                        <p class="pTitle">${this.schoolName}卫生材料领用单</p>
                        <div class="table-header">
                            <div>NO.${currentNumber}</div> <!-- 页码递增 -->
                            <div>第 ${pageIndex + 1} 页/共 ${totalPages} 页</div>
                        </div>
                        <div class="table-info">
                            <span>库房: 物资库房</span>
                            <span>领用科室: ${department}</span>
                            <span>领用人: ${requestor}</span>
                            <span> </span>
                        </div>
                        <table>
                            <thead>
                                <tr>
                                    <td>材料信息</td>
                                    <td>材料分类</td>
                                    <td>规格</td>
                                    <td>单位</td>
                                    <td>数量</td>
                                    <td>售价</td>
                                    <td>售价金额</td>
                                </tr>
                            </thead>
                            <tbody>
                                ${pageItems.map(item => `
                                    <tr>
                                        <td>${item.materialName}</td>
                                        <td>${item.categoriesName}</td>
                                        <td>${item.printSpec}</td>
                                        <td>${item.printUnit}</td>
                                        <td>${item.quantity}</td>
                                        <td>${item.printPrice}</td>
                                        <td>${item.printMoney}</td> 
                                    </tr>
                                `).join('')}
                            </tbody>
                        </table>
                        <div class="table-footer">
                            <div>摘要</div>
                            <div>填制人: ${createName}</div>
                            <div>填制日期: ${createdAt}</div>
                            <div>领料人签字: &emsp; &emsp; &emsp; &emsp; &emsp; &emsp;</div>
                        </div>
                    </div>
                    <div style="page-break-after: always;"></div> <!-- 确保分页 -->
                                `;
                }).join('');

                // 创建打印窗口
                const WinPrint = window.open('', '', 'width=800,height=600');
                WinPrint.document.write(`
                    <html>
                    <head>
                        <title>打印</title>
                        <style>
                            @media print { 
                                @page {
                                    margin: 30px 20mm;
                                }
                                body {
                                    margin: 0;
                                    font-family: Arial, sans-serif;
                                }
                                .table-container {
                                    width: 100%;
                                    text-align: center;
                                    page-break-before: always; /* 强制每个新内容开始时分页 */
                                }
                                .table-container:last-child {
                                    page-break-after: auto; /* 最后一页不分页 */
                                }
                                .table-header,
                                .table-info,
                                .table-footer {
                                    display: flex;
                                    justify-content: space-between; 
                                }
                                .pTitle {
                                    font-size: 20px;
                                    font-weight: 400;
                                    text-align: center;
                                }
                                table {
                                    width: 100%;
                                    border: 1px solid #000;
                                    border-collapse: collapse;
                                }
                                th, td {
                                    border: 1px solid #000; 
                                    text-align: center;
                                }
                                tr:nth-child(even) {
                                    background-color: #f2f2f2; /* 隔行变色 */
                                }
                            }
                        </style>
                    </head>
                    <body>
                        ${printContent}
                    </body>
                    </html>
                `);
                WinPrint.document.close();
                WinPrint.print();
                WinPrint.close();
            } else {
                ElMessage.error("请选择要打印的出库单!");
            }
        },



        tableRowClassName({ row, rowIndex }) {
            // console.log(rowIndex);
            return rowIndex % 2 === 1 ? 'warning-row' : '';
        },
        headerCellStyle() {
            return {
                background: '#f2f2f2',
            };
        },
        tableRowStyle({ row, rowIndex }) {
            if (rowIndex % 2 === 1) {
                return {
                    background: '#F5F5F5',
                };
            }
        },
        selectHandle(value) {
            console.log(value);
            this.searchTitle = value
        },
        changePageSize(size) {
            this.pageSize = size;
            this.getMylist()
        },
        changeCurrentPage(page) {
            this.page = page;
            this.getMylist()
        },
        exportExcel() {
            if (this.searchValue) {
                    this.requestor = this.searchValue
            }else{
                this.requestor=""
            }
             if(this.searchValue2){
                this.materialName = this.searchValue2
            } else{
                this.materialName = ""
            }
            this.fullscreenLoading = true
            httpService(
                "/javaApi/stockOut/exportExcel", {
                requestor: this.requestor,
                materialName: this.materialName
            }, "get"
            ).then((data) => {
                this.fullscreenLoading = false
                window.open(data.message, "_blank")
            });
        },

        getMylist() {
            if (this.searchValue) {
                    this.requestor = this.searchValue
            }else{
                this.requestor=""
            }
             if(this.searchValue2){
                this.materialName = this.searchValue2
            } else{
                this.materialName = ""
            }
            httpService(`/javaApi/stockOut/list/${this.page}/${this.pageSize}`, {
                requestor: this.requestor,
                materialName: this.materialName
            }, "get").then(
                (data) => {
                    if (data.list) {
                        // data.list.map(item => item.createdAt = formatTime(new Date(item.createdAt)))
                        this.list = data.list
                        this.total = data.total
                        console.log(this.list);
                    }
                }
            );
            // httpService(`/javaApi/stockOutReceipt/list/${this.page}/${this.pageSize}`, {}, "get").then(
            //     (data) => {
            //         if (data.list) {
            //             data.list.map(item => item.createdAt = formatTime(new Date(item.createdAt)))
            //             this.list = data.list
            //             this.total = data.total
            //         }
            //     }
            // );
        },
        viewDetails(data) {
            console.log(123);
            this.$refs.MedicalPrintListView.flag = true
            this.$refs.MedicalPrintListView.form = data
            this.$refs.MedicalPrintListView.queryById(data.id)
        },


    },
};
</script>
<style scoped>
* {
    padding: 0;
    margin: 0;
}

.flex {
    display: flex;
    align-items: center;
}



.header {
    height: 45px;
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.header_btn {
    width: 99px;
    height: 32px;
    background-color: #6F72FF;
    border-radius: 5px;
    justify-content: center;
    font-size: 14px;
    color: #fff;
    margin-right: 15px;
    cursor: pointer;
}

.header_btn img {
    width: 17px;
    height: 18px;
    margin-right: 5px;
}

.serachBox {
    color: #666666;
    font-size: 14px;
    width: 353px;
    height: 36px;
    border: 1px solid #D0D0D0;
    box-sizing: border-box;
    border-radius: 20px;
    margin-right: 15px;
}

.serachBox .flex {
    justify-content: center;
    border-right: 1px solid #D0D0D0;
    margin-right: 10px;
    padding: 0 5px 0 15px;
    color: #666666;
    cursor: pointer;
}

.serachBox input {
    height: calc(100% - 4px);
    border: none;
    flex: 1;
    margin-right: 15px;
    outline: none;
}

.search {
    width: 59px;
    height: 30px;
    border-radius: 15px;
    border: none;
    background-color: #6F72FF;
    color: #fff;
    font-size: 14px;
    margin-right: 2px;
    outline: none;
}


.borderBox {
    height: calc(100% - 60px);
    border: 1px solid #DFDFDF;
    border-radius: 5px;
    box-sizing: border-box;
    margin-top: 15px;
}

.borderTop {
    height: 40px;
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.workList {
    display: flex;
}

.workList p {
    color: #666;
    font-size: 14px;
    margin-left: 20px;
}

.workList p span {
    color: #6F72FF;
}

.borderTop .flex {
    margin-right: 20px;
}

.borderTop_btn {
    display: flex;
    align-items: center;
    font-size: 14px;
    color: #666666;
    margin-left: 40px;
    cursor: pointer;
}

.header_icon {
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.table_box {
    width: calc(100vw - 283px);
    height: calc(100% - 0px);
    overflow-y: auto;
}

.el-table {}

.rotate {
    transform: rotate(90deg);
}


.el-scrollbar__bar {
    z-index: 11;
    overflow-x: hidden;
    position: sticky;
}

.el-table.el-table--border {
    overflow: unset;
}


.pageBox {
    background-color: #f6f6f6;
    padding: 5px 0;
    border-top: 1px solid #e7e7e7;
    border-radius: 0 0 5px 5px;
}

.el-pagination {
    margin-left: 10px;
}

.pageBox {
    justify-content: space-between;
}

.pageBox .flex {
    color: #666;
    font-size: 14px;
    margin-right: 10px;
}

.pageBox .flex p {
    color: #666;
    font-size: 14px;
    margin-right: 20px;
}

.addbtn {
    width: 110px;
    height: 32px;
    border-radius: 5px;
    border: none;
    background-color: #6F72FF;
    color: #fff;
    font-size: 14px;
    margin-left: 15px;
    cursor: pointer;
    outline: none;
    line-height: 1;
    justify-content: center;
    margin-right: 15px;
}

.el-tooltip__trigger:focus-visible {
    outline: unset;
}
</style>